package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomInfoService roomInfoService;

    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    DistrictInfoService districtInfoService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;

    @Override
    public void customsaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        Long id = apartmentSubmitVo.getId();
        boolean isUpdate = id == null && id!= 0;
        //查询下省市区的名称
        String provinceName = provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName();
        String cityName = cityInfoService.getById(apartmentSubmitVo.getCityId()).getName();
        String districtName = districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName();
        apartmentSubmitVo.setProvinceName(provinceName);
        apartmentSubmitVo.setCityName(cityName);
        apartmentSubmitVo.setDistrictName(districtName);
        saveOrUpdate(apartmentSubmitVo);

        if(isUpdate){
            //删除
            LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
            apartmentLabelService.remove(labelLambdaQueryWrapper);
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
            apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);
            LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
            apartmentFacilityService.remove(facilityLambdaQueryWrapper);
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            //插入
            List<Long> labelIds = apartmentSubmitVo.getLabelIds();
            if(!CollectionUtils.isEmpty(labelIds)){
                ArrayList<ApartmentLabel> apartmentLabelArrayListList = new ArrayList<>(labelIds.size());
                for (Long labelId : labelIds) {
                    ApartmentLabel label = ApartmentLabel.builder().apartmentId(id).labelId(labelId).build();
                    apartmentLabelArrayListList.add(label);
                }
                apartmentLabelService.saveBatch(apartmentLabelArrayListList);
            }
            List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
            if(!CollectionUtils.isEmpty(feeValueIds)){
                ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>(feeValueIds.size());
                for (Long feeValueId : feeValueIds) {
                    ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(id).feeValueId(feeValueId).build();
                    apartmentFeeValues.add(apartmentFeeValue);
                }
                apartmentFeeValueService.saveBatch(apartmentFeeValues);
            }
            //公寓配套中间表信息
            List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
            if (!CollectionUtils.isEmpty(facilityInfoIds)){
                List<ApartmentFacility> apartmentFacilityList = new ArrayList<>(facilityInfoIds.size());
                for (Long facilityInfoId : facilityInfoIds) {
                    ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(id).facilityId(facilityInfoId).build();
                    apartmentFacilityList.add(apartmentFacility);
                }
                apartmentFacilityService.saveBatch(apartmentFacilityList);
            }
            //图片
            List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
            if(!CollectionUtils.isEmpty(graphVoList)){
                ArrayList<GraphInfo> graphInfos = new ArrayList<>(graphVoList.size());
                for (GraphVo graphVo : graphVoList) {
                    GraphInfo graphInfo = new GraphInfo();
                    graphInfo.setName(graphVo.getName());
                    graphInfo.setUrl(graphVo.getUrl());
                    graphInfo.setItemId(id);
                    graphInfo.setItemType(ItemType.APARTMENT);
                    graphInfos.add(graphInfo);
                }
                graphInfoService.saveBatch(graphInfos);
            }

        }

    }

    @Override
    public ApartmentDetailVo customDetail(Long id) {
        ApartmentInfo apartmentInfo = getById(id);
        if(apartmentInfo == null){
            return null;
        }
        List<GraphVo> graphVoList = graphInfoMapper.queryVo(id,ItemType.APARTMENT);
        List<LabelInfo> labelInfoList = labelInfoMapper.queryInfo(id);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.queryInfo(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.queryVo(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        return apartmentDetailVo;

    }

    @Override
    public void customRemoveById(Long id) {
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(lambdaQueryWrapper);
        if(count > 0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

        removeById(id);
    }


    @Override
    public void findPage(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        apartmentInfoMapper.queryPage(page,queryVo);
    }
}




